package processor;

import java.awt.Point;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import ui.ChatWin;

public class UpdateChatWin {
	private ChatWin chatWin;
	
	public UpdateChatWin(JFrame chatWin){
		this.chatWin = (ChatWin) chatWin;
	}
	
	public void quit(){
		chatWin.dispose();
		System.exit(0);
	}
	
	public void nofityServerCrash(Date time) {
		SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
		String timeString = sdf.format(time);
		chatWin.getChatArea().append("\n ["+timeString+"] Server crashed.");
		
		chatWin.getChatPane().getViewport().add(chatWin.getChatArea(), null);
		int height=10;
		Point p = new Point();
		p.setLocation(0,chatWin.getChatArea().getLineCount()*height);
		chatWin.getChatPane().getViewport().setViewPosition(p);
		
		chatWin.setCanSendMsg(false);
	}
	
	public void updateChatMsg(String msg, String from,String to, Date time){
		SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
		String timeString = sdf.format(time);
		if(to == null){
			to = "All";
		}
		chatWin.getChatArea().append("\n ["+timeString+"] "+from+" send to "+to+": "+msg);
		
		chatWin.getChatPane().getViewport().add(chatWin.getChatArea(), null);
		int height=10;
		Point p = new Point();
		p.setLocation(0,chatWin.getChatArea().getLineCount()*height);
		chatWin.getChatPane().getViewport().setViewPosition(p);
	}
	
	public void shakeWindow(String from, String to, Date time){
		SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
		String timeString = sdf.format(time);
		chatWin.shake();
		if (to == null) {
			chatWin.getChatArea().append("\n ["+timeString+"] "+from+" shake ALL!");
		} else {
			chatWin.getChatArea().append("\n ["+timeString+"] "+from+" shake you!");
		}
		chatWin.getChatArea().setCaretPosition(chatWin.getChatArea().getText().length());
	}
	
	public void addUserList(List<String> list){
		DefaultListModel<String> users = chatWin.getUsers();
		users.removeAllElements();
		users.addElement("ALL");
		for(String s:list){
			users.addElement(s);
		}
		
		if (chatWin.getUserList().getSelectedIndex() == -1) {
			chatWin.getUserList().setSelectedIndex(0);
		}
	}
	
	public void addUser(String from, Date time){
		SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
		String timeString = sdf.format(time);
		DefaultListModel<String> users = chatWin.getUsers();
		users.addElement(from);
		chatWin.getChatArea().append("\n ["+timeString+"] "+from+": is online! ");
		chatWin.getChatArea().setCaretPosition(chatWin.getChatArea().getText().length());
	}
	
	public void removeUser(String from, Date time){
		SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
		String timeString = sdf.format(time);
		DefaultListModel<String> users = chatWin.getUsers();
		users.removeElement(from);
		
		if (chatWin.getUserList().getSelectedIndex() == -1) {
			chatWin.getUserList().setSelectedIndex(0);
		}
		
		chatWin.getChatArea().append("\n ["+timeString+"] "+from+": is off-line! ");
		chatWin.getChatArea().setCaretPosition(chatWin.getChatArea().getText().length());
	}
	
}
